package com.freedom.admin.domain.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.freedom.admin.domain.repository.RoleMenuRepository;
import com.freedom.admin.domain.service.RoleMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class RoleMenuServiceImpl implements RoleMenuService {

    private final RoleMenuRepository roleMenuRepository;

    @Override
    public void removeByRoleIds(List<Integer> roleIds) {
        roleMenuRepository.removeByRoleIds(roleIds);
    }

    @Override
    public void addRoleMenu(Integer roleId, List<Integer> menuIds) {
        List<Integer> menuIdList = roleMenuRepository.findMenuIdsByRoleId(roleId);
        if (CollUtil.isNotEmpty(menuIdList)) {
            List<Integer> containIdList = menuIdList.stream().filter(menuIds::contains).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(containIdList)) {
                roleMenuRepository.removeRoleMenu(roleId, containIdList);
            }
        }
        roleMenuRepository.addRoleMenu(roleId, menuIds);
    }
}
